//
//  ViewController.m
//  MagicCube
//
//  Created by 蓓蕾 on 2021/12/31.
//

#import "ViewController.h"

#import "VideoViewController.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    [self getDataSource];
    [self initTableView];
}

-(void)getDataSource
{
    NSString *videoPathOne = [[NSBundle mainBundle] pathForResource:@"第一讲.mp4" ofType:nil];
    NSString *videoPathTwo = [[NSBundle mainBundle] pathForResource:@"第二讲.mp4" ofType:nil];
    NSString *videoPathThree = [[NSBundle mainBundle] pathForResource:@"第三讲.mp4" ofType:nil];
    NSString *videoPathFour = [[NSBundle mainBundle] pathForResource:@"第四讲.mp4" ofType:nil];
    NSString *videoPathFive = [[NSBundle mainBundle] pathForResource:@"第五讲.mp4" ofType:nil];
    NSString *videoPathSix = [[NSBundle mainBundle] pathForResource:@"第六讲.mp4" ofType:nil];
    NSString *videoPathSeven = [[NSBundle mainBundle] pathForResource:@"第七讲.mp4" ofType:nil];

    _dataArr = [NSMutableArray new];
    [_dataArr addObject:videoPathOne];
    [_dataArr addObject:videoPathTwo];
    [_dataArr addObject:videoPathThree];
    [_dataArr addObject:videoPathFour];
    [_dataArr addObject:videoPathFive];
    [_dataArr addObject:videoPathSix];
    [_dataArr addObject:videoPathSeven];
}

-(void)initTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (indexPath.row < _dataArr.count) {
        NSString *videoPath = _dataArr[indexPath.row];
        NSString *videoName = [videoPath stringByDeletingPathExtension];
        videoName = [videoName lastPathComponent];
        cell.textLabel.text = videoName;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *videoPath = _dataArr[indexPath.row];
    NSString *videoName = [videoPath stringByDeletingPathExtension];
    videoName = [videoName lastPathComponent];
    VideoViewController *videoVC = [[VideoViewController alloc] init];
    videoVC.navigationItem.title = @"视频详情";
    videoVC.videoPath = videoPath;
    videoVC.videoName = videoName;
    videoVC.videoID = indexPath.row + 1;
    [self.navigationController pushViewController:videoVC animated:YES];
}


@end
